using Engine.Graphics.VertexStructures;
using SharpDX;
using System.IO;

namespace Engine.FileFormats3D.C11Undercover
{
    internal class C11VertexBlock
    {
        #region Private Fields

        private MyOwnVertex.MyPositionNormalTextured[] engineVertices;
        private C11Vertex[] vertices;

        #endregion Private Fields

        #region Internal Constructors

        internal C11VertexBlock()
        { }

        #endregion Internal Constructors

        #region Public Properties

        public MyOwnVertex.MyPositionNormalTextured[] EngineVertices
        {
            get { return engineVertices; }
            set { engineVertices = value; }
        }

        #endregion Public Properties

        #region Internal Properties

        internal C11Vertex[] Vertices
        {
            get { return vertices; }
            set { vertices = value; }
        }

        #endregion Internal Properties

        #region Internal Methods

        internal static C11VertexBlock FromStream(BinaryReader br, int numberOfVertices)
        {
            C11VertexBlock block = new C11VertexBlock();
            block.Vertices = new C11Vertex[numberOfVertices];
            block.engineVertices = new MyOwnVertex.MyPositionNormalTextured[numberOfVertices];

            for (int i = 0; i < block.Vertices.Length; i++)
            {
                block.Vertices[i] = C11Vertex.FromStream(br);
                C11Vertex c11Vertex = block.Vertices[i];

                Color4 vertexColor = new Color4((int)c11Vertex.VertexColor);
                // switch blue channel with red.
                //float temp = vertexColor.Blue;
                //vertexColor.Blue = vertexColor.Red;
                //vertexColor.Red = temp;

                block.engineVertices[i] = new MyOwnVertex.MyPositionNormalTextured(
                    // Position
                    new Vector3(c11Vertex.X, c11Vertex.Y, c11Vertex.Z),
                    // Normal
                    Vector3.Normalize(new Vector3(c11Vertex.NormalX, c11Vertex.NormalY, c11Vertex.NormalZ)),
                    // Texture Coordinates #1
                    c11Vertex.TextureU1, c11Vertex.TextureV1,
                    // Texture Coordinates #2
                    c11Vertex.TextureU2, c11Vertex.TextureV2,
                    // diffuse Color
                    c11Vertex.DiffuseColor,
                    // VertexColor w x y z Alpha red green blue
                    vertexColor);

                //byte[] bytes = BitConverter.GetBytes( (uint)c11Vertex.Data2);
                ////sbyte[] sBytes = new sbyte[bytes.Length];

                ////for (int j = 0; j < bytes.Length; j++)
                ////{
                ////    sBytes[j] = (sbyte)(bytes[j] -128);
                ////}

                //block.engineVertices[i].vertexColorC11 = (uint)(bytes[0] + bytes[1] << 8 + bytes[2] << 16 + bytes[3] << 24);
            }

            return block;
        }

        #endregion Internal Methods
    }
}